package operation;

import book.BookList;

import java.util.Scanner;

import book.Book;

public class DeleteOperation implements IOperation{

    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的书名");
        String name = scanner.next();
        int currentSize = bookList.getUsedSize();
        int index = -1;
        int i = 0;
        for (; i < currentSize; i++) {
            Book tmp = bookList.getBooks(i);
            if(tmp.getName().equals(name)) {
                //记录当前下标
                index = i;
                break;
            }
        }
        if(i >= currentSize) {
            System.out.println("没有你要删除的图书");
            return;
        }
        //开始删除
        for (int j = index; j < currentSize-1; j++) {
            Book book = bookList.getBooks(j+1);
            bookList.setBooks(book,j);
        }
        //把最后一本书置为空
        bookList.setBooks(null,currentSize-1);
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功");
    }
}
